<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="产品名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="产品名称"></el-input>
    </el-form-item>
    <!--<el-form-item label="产品key" prop="productKey">-->
      <!--<el-input v-model="dataForm.productKey" placeholder="产品key"></el-input>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="产品密钥" prop="productSecret">-->
      <!--<el-input v-model="dataForm.productSecret" placeholder="产品密钥"></el-input>-->
    <!--</el-form-item>-->
    <el-form-item label="产品描述" prop="details">
      <el-input v-model="dataForm.details" placeholder="产品描述"></el-input>
    </el-form-item>
    <!--<el-form-item label="创建id" prop="userId">-->
      <!--<el-input v-model="dataForm.userId" placeholder="创建id"></el-input>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="创建时间" prop="createDate">-->
      <!--<el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="修改时间" prop="modifyDate">-->
      <!--<el-input v-model="dataForm.modifyDate" placeholder="修改时间"></el-input>-->
    <!--</el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          productKey: '',
          productSecret: '',
          details: '',
          userId: '',
          createDate: '',
          modifyDate: ''
        },
        dataRule: {
          name: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          productKey: [
            { required: true, message: '产品key不能为空', trigger: 'blur' }
          ],
          productSecret: [
            { required: true, message: '产品密钥不能为空', trigger: 'blur' }
          ],
          details: [
            { required: true, message: '产品描述不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '创建id不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyDate: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tproduct/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tproduct.name
                this.dataForm.productKey = data.tproduct.productKey
                this.dataForm.productSecret = data.tproduct.productSecret
                this.dataForm.details = data.tproduct.details
                this.dataForm.userId = data.tproduct.userId
                this.dataForm.createDate = data.tproduct.createDate
                this.dataForm.modifyDate = data.tproduct.modifyDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tproduct/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'productKey': this.dataForm.productKey,
                'productSecret': this.dataForm.productSecret,
                'details': this.dataForm.details,
                'userId': this.dataForm.userId,
                'createDate': this.dataForm.createDate,
                'modifyDate': this.dataForm.modifyDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
